<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

<script>
// 数据类型转换通常指将其他数据类型转成 字符串 数字 布尔值
// 数据类型转换有两种: 强制转换(显式转换) 和 自动转换(隐式转换)

// 强制转换(显式转换): 其他类型 转成 数字
var str1 = '123';
var str2 = 'hello';
var num = 123;
var boo = true;
var nul = null;
var und = undefined;
var obj = {a:1,b:2};
var fun = function (){};
var arr1 = [];
var arr2 = [6];
var arr3 = [7,8,9];

/* // Number(val)将val转成数字类型
console.log( Number(str1) );// 123
console.log( Number(str2) );// NaN
console.log( Number(num) );// 123
console.log( Number(boo) );// 1
console.log( Number(nul) );// 0
console.log( Number(und) );// NaN
console.log( Number(obj) );// NaN
console.log( Number(fun) );// NaN
console.log( Number(arr1) );// 0
console.log( Number(arr2) );// 6
console.log( Number(arr3) );// NaN */

// parseInt() 和 parseFloat() 也可以将数字或字符串转成数字
console.log( parseInt(str1) );// 123
console.log( parseInt(str2) );// NaN
console.log( parseInt(boo) );// NaN
console.log( parseInt(nul) );// NaN
console.log( parseInt(und) );// NaN

console.log( parseFloat(str1) );// 123
console.log( parseFloat(str2) );// NaN
console.log( parseFloat(boo) );// NaN
console.log( parseFloat(nul) );// NaN
console.log( parseFloat(und) );// NaN

</script>
</body>
</html>